package com.app.xinfadi.ui.purchase_price;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.app.xinfadi.R;
import com.app.xinfadi.base.BaseActivity;
import com.app.xinfadi.dao.HomeDao;
import com.app.xinfadi.dao_bean.bean.CenterPurchasePlanBean;
import com.app.xinfadi.dao_bean.bean.refresh_bean.center_role.CenterRoleExamineAndApproveListRefreshBean;
import com.app.xinfadi.dao_bean.bean.refresh_bean.lead_role.LeadRoleRefreshBean;
import com.app.xinfadi.ui.MainActivity;
import com.app.xinfadi.ui.home.view.GridViewImageNomalAdapter;
import com.app.xinfadi.ui.purchase_plan.center_purchase_plan.CenterPurchasePlanDetailActrvity;
import com.app.xinfadi.util.Toast;
import com.app.xinfadi.widget.MyGridView;
import com.app.xinfadi.widget.dialog.EndPlanDialog;
import com.app.xinfadi.widget.dialog.ExamineAndVerifyDialog;
import com.app.xinfadi.widget.gson.Success;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * function:价格报价
 * Created by Wutengfei on 2016/1/26.
 */
public class PurchasePriceDetailsActivity extends BaseActivity {
    private Button newBillOfSalesBtn;
    private Button examineAndApproveBtn;
    /**
     * 0 采购员
     * 1 中心
     */
    private int type;
    private String status = "-1";
    private String plan_id;
    //市场
    private TextView producingAreaRbtn;
    /*基地*/
    private TextView homeBaseTv;
    /*产品名称*/
    private TextView productNameTv;
    /*可采数量*/
    private TextView purchaseCountTv;
    /*规格*/
    private TextView purchaseUnitTv;
    /*报价有效期*/
    private TextView expiryDateTv;
    /*交货时间*/
    private TextView deliveryTimeTv;
    /*价格区间*/
    private TextView spriceTv;
    private TextView epriceTv;
    /*制定人*/
    private TextView buyerTv;
    /*计划发起时间*/
    private TextView planCreateTimeTv;
    /*品质*/
    private TextView qualityTv;
    /*品牌*/
    private TextView brandTv;
    //基地图片
    private MyGridView photoGridView;
    private TextView noImageTv;
    private CenterPurchasePlanBean centerPurchasePlanBean;
    private Button recallBtnBtn;
    private Button endBtnBtn;
    private ExamineAndVerifyDialog examineAndVerifyDialog;
    /*撤回*/
    private EndPlanDialog ReCallDialog;
    /*终止*/
    private EndPlanDialog endDialog;
    private TextView markerTv;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        setContentView(R.layout.purchase_price_detail_layout_activity);
        getBundle();
        initToolbar();
        initView();
        initData();
    }

    private void getBundle() {
        type = getIntent().getIntExtra("type", 0);
        status = getIntent().getStringExtra("status");
        if (TextUtils.isEmpty(status)) {
            status = "-1";
        }
        centerPurchasePlanBean = (CenterPurchasePlanBean) getIntent().getSerializableExtra("data");
        plan_id = centerPurchasePlanBean.PLAN_ID;
    }

    private void initView() {
        newBillOfSalesBtn = (Button) findViewById(R.id.newBillOfSalesBtn);
        newBillOfSalesBtn.setOnClickListener(this);
        examineAndApproveBtn = (Button) findViewById(R.id.examineAndApproveBtn);
        examineAndApproveBtn.setOnClickListener(this);
        recallBtnBtn = (Button) findViewById(R.id.recallBtnBtn);
        recallBtnBtn.setOnClickListener(this);
        endBtnBtn = (Button) findViewById(R.id.endBtnBtn);
        endBtnBtn.setOnClickListener(this);
        if (type == 0) {
            if (status.equals("01")) {
                recallBtnBtn.setVisibility(View.VISIBLE);
            }
        } else if (type == 1) {
            if (status.equals("01")) {
                examineAndApproveBtn.setVisibility(View.VISIBLE);
            } else if (status.equals("11")) {
                recallBtnBtn.setVisibility(View.VISIBLE);
            }
        } else if (type == 2) {
            if (status.equals("11")) {
                examineAndApproveBtn.setVisibility(View.VISIBLE);
            } else if (status.equals("21")) {
                /*终止*/
                endBtnBtn.setVisibility(View.VISIBLE);
            } else if (status.equals("03")) {

            }
        }


        producingAreaRbtn = (TextView) findViewById(R.id.producingAreaRbtn);
        homeBaseTv = (TextView) findViewById(R.id.homeBaseTv);
        productNameTv = (TextView) findViewById(R.id.productNameTv);
        purchaseCountTv = (TextView) findViewById(R.id.purchaseCountTv);
        purchaseUnitTv = (TextView) findViewById(R.id.purchaseUnitTv);
        expiryDateTv = (TextView) findViewById(R.id.expiryDateTv);
        deliveryTimeTv = (TextView) findViewById(R.id.deliveryTimeTv);
        buyerTv = (TextView) findViewById(R.id.buyerTv);
        planCreateTimeTv = (TextView) findViewById(R.id.planCreateTimeTv);
        qualityTv = (TextView) findViewById(R.id.qualityTv);
        brandTv = (TextView) findViewById(R.id.brandTv);
        photoGridView = (MyGridView) findViewById(R.id.photoGridView);
        spriceTv = (TextView) findViewById(R.id.spriceTv);
        epriceTv = (TextView) findViewById(R.id.epriceTv);
        noImageTv = (TextView) findViewById(R.id.noImageTv);
        markerTv = (TextView) findViewById(R.id.markerTv);
    }

    private void initData() {
//        producingAreaRbtn.setText(centerPurchasePlanBean.);
        if (centerPurchasePlanBean != null) {
            if (!TextUtils.isEmpty(centerPurchasePlanBean.PLACE_NAME))
                producingAreaRbtn.setText(centerPurchasePlanBean.PLACE_NAME);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.SITE_NAME))
                homeBaseTv.setText(centerPurchasePlanBean.SITE_NAME);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.PRODUCTS_NAME))
                productNameTv.setText(centerPurchasePlanBean.PRODUCTS_NAME);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.PURCHASE_COUNT))
                purchaseCountTv.setText(centerPurchasePlanBean.PURCHASE_COUNT);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.STANDARD))
                purchaseUnitTv.setText(centerPurchasePlanBean.STANDARD);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.EPRICE))
                expiryDateTv.setText(centerPurchasePlanBean.EPRICE);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.DELIVERY_TIME))
                deliveryTimeTv.setText(centerPurchasePlanBean.DELIVERY_TIME);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.SPRICE))
                spriceTv.setText(centerPurchasePlanBean.SPRICE);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.EPRICE))
                epriceTv.setText(centerPurchasePlanBean.EPRICE);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.MAKER))
                markerTv.setText(centerPurchasePlanBean.MAKER);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.BUYER))
                buyerTv.setText(centerPurchasePlanBean.BUYER);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.PLAN_CREATE_TIME))
                planCreateTimeTv.setText(centerPurchasePlanBean.PLAN_CREATE_TIME);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.QUALITY))
                qualityTv.setText(centerPurchasePlanBean.QUALITY);
            if (!TextUtils.isEmpty(centerPurchasePlanBean.BRAND))
                brandTv.setText(centerPurchasePlanBean.BRAND);
            if (centerPurchasePlanBean.PIC != null && centerPurchasePlanBean.PIC.length() > 0) {
                List<String> images = new ArrayList<String>();
                String imageStrs[] = centerPurchasePlanBean.PIC.split(";");
                images = Arrays.asList(imageStrs);
//        photoGridView = (MyGridView) findViewById(R.id.photoGridView);
                GridViewImageNomalAdapter gridViewImageNoAddAdapter = new GridViewImageNomalAdapter(context, images);
                photoGridView.setAdapter(gridViewImageNoAddAdapter);
            } else {
                noImageTv.setVisibility(View.VISIBLE);
            }
        }
        examineAndVerifyDialog = new ExamineAndVerifyDialog(context);
        //审核
        examineAndVerifyDialog.setSuggestionOnclickListener(new ExamineAndVerifyDialog.SuggestionOnclickListener() {
            @Override
            public void onClick(View v, String suggestion) {
                //不通过
                if (type == 1) {
                    status = "13";
                } else if (type == 2) {
                    status = "22";
                }
                HomeDao homeDao = new HomeDao();
                homeDao.updatePlans(status, plan_id, suggestion, new Response.Listener<Success>() {
                    @Override
                    public void onResponse(Success response) {
                        Toast.makeText(context, "审批成功！", Toast.LENGTH_LONG).show();
                        examineAndVerifyDialog.dismiss();
                        if (type == 1) {
                            EventBus.getDefault().post(new CenterRoleExamineAndApproveListRefreshBean(true));
                        } else if (type == 2) {
                            EventBus.getDefault().post(new LeadRoleRefreshBean(true));
                        }
                        popAllActivityExceptOne(MainActivity.class);

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
//                        Toast.makeText(context, "审批成功！", Toast.LENGTH_LONG).show();

                        examineAndVerifyDialog.dismiss();
                    }
                });
            }
        }, new ExamineAndVerifyDialog.SuggestionOnclickListener() {
            @Override
            public void onClick(View v, String suggestion) {
                /*通过*/
                if (type == 1) {
                    status = "11";
                } else if (type == 2) {
                    status = "21";
                }
                HomeDao homeDao = new HomeDao();
                homeDao.updatePlans(status, plan_id, suggestion, new Response.Listener<Success>() {
                    @Override
                    public void onResponse(Success response) {
                        Toast.makeText(context, "审批成功！", Toast.LENGTH_LONG).show();
                        examineAndVerifyDialog.dismiss();
                        if (type == 1) {
                            EventBus.getDefault().post(new CenterRoleExamineAndApproveListRefreshBean(true));
                        } else if (type == 2) {
                            EventBus.getDefault().post(new LeadRoleRefreshBean(true));
                        }
                        popAllActivityExceptOne(MainActivity.class);

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
//                        Toast.makeText(context, "审批成功！", Toast.LENGTH_LONG).show();

                        examineAndVerifyDialog.dismiss();
                    }
                });
            }
        });
        ReCallDialog = new EndPlanDialog(context, "撤回");
        ReCallDialog.setSuggestionOnclickListener(new EndPlanDialog.SuggestionOnclickListener() {
            @Override
            public void onClick(View v, String suggestion) {
                if (type == 1) {
                    status = "12";
                }
                HomeDao homeDao = new HomeDao();
                homeDao.updatePlans(status, plan_id, suggestion, new Response.Listener<Success>() {
                    @Override
                    public void onResponse(Success response) {
                        Toast.makeText(context, "撤回成功！", Toast.LENGTH_LONG).show();
                        examineAndVerifyDialog.dismiss();
                        if(type==1){
                            EventBus.getDefault().post(new CenterRoleExamineAndApproveListRefreshBean(true));
                        }
                        popAllActivityExceptOne(MainActivity.class);

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(context, "提交失败！", Toast.LENGTH_LONG).show();

                    }
                });
            }
        });
        endDialog = new EndPlanDialog(context, "终止");
        endDialog.setSuggestionOnclickListener(new EndPlanDialog.SuggestionOnclickListener() {
            @Override
            public void onClick(View v, String suggestion) {
                HomeDao homeDao = new HomeDao();
                homeDao.updatePlans("23", plan_id, suggestion, new Response.Listener<Success>() {
                    @Override
                    public void onResponse(Success response) {
                        Toast.makeText(context, "终止成功！", Toast.LENGTH_LONG).show();
                        examineAndVerifyDialog.dismiss();
                        EventBus.getDefault().post(new LeadRoleRefreshBean(true));
                        popAllActivityExceptOne(MainActivity.class);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Toast.makeText(context, "终止失败！", Toast.LENGTH_LONG).show();

                    }
                });
            }
        });
    }

    private void initToolbar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        TextView title = (TextView) findViewById(R.id.titleTv);
        title.setVisibility(View.VISIBLE);
        title.setText("采购报价详情");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.newBillOfSalesBtn:
                Intent intent = new Intent(context, CenterPurchasePlanDetailActrvity.class);
                startActivity(intent);
                break;
            case R.id.examineAndApproveBtn:

                if (type == 1 || type == 2) {
                    examineAndVerifyDialog.show();
                }
                break;
            case R.id.endBtnBtn:
                if (type == 2) {
                    endDialog.show();
                }
                break;
        }
    }
}
